﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Brain;
namespace AvaProvi
{
    public partial class ctlEMVentas : AvaProvi.ControlBase
    {
        VSimbolo vs;
        Validadora val;
        public ctlEMVentas()
            : base()
        {
            InitializeComponent();
            ActualizaPanel();
        }

        public override void ActualizaDatos()
        {
            base.ActualizaDatos();
            this.bsEnfoque.EndEdit();
            clsAvaluo_CRN.AvaluoActual.Guardar();
            this.ActualizaPanel();
        }

        public override void ActualizaPanel()
        {
            this.bsEnfoque.DataSource = clsAvaluo_CRN.AvaluoActual.Inmueble.EnfoqueDeMercado;
            this.bsEnfoqueVenta.DataSource = clsAvaluo_CRN.AvaluoActual.Inmueble.EnfoqueDeMercado.ComparacionesEnfoqueMVenta;
            this.bsEnfoqueVenta.ResetBindings(false);
            bsEnfoque.ResetBindings(false);
            this.bsConstruccion.DataSource = clsAvaluo_CRN.AvaluoActual.Inmueble.Terreno.Construccion;
            this.bsConstruccion.ResetBindings(false);
            this.baseUltraGroupBox.Text = "Enforque de Mercado:Ventas(Terreno de: " + Math.Round(clsAvaluo_CRN.AvaluoActual.Inmueble.EnfoqueDeMercado.ValorDeTerrenoPermitidoDesde,0) + "m² a " + Math.Round(clsAvaluo_CRN.AvaluoActual.Inmueble.EnfoqueDeMercado.ValorDeTerrenoPermitidoHasta,0) + "; Construcción de: " + Math.Round(clsAvaluo_CRN.AvaluoActual.Inmueble.EnfoqueDeMercado.ValorDeConstruccionPermitidoDesde,0) + "m² a " + Math.Round(clsAvaluo_CRN.AvaluoActual.Inmueble.EnfoqueDeMercado.ValorDeConstruccionPermitidoHasta,0) + "m²)";
        }

        private void gvPrecios_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            this.bsEnfoque.ResetBindings(false);
            this.bsEnfoqueVenta.ResetBindings(false);
            this.bsConstruccion.ResetBindings(false);
        }

        private void btnCopiar_Click(object sender, EventArgs e)
        {
            if (bsEnfoqueVenta.Current != null)
            {
                clsEnfoqueMercadoVenta_CRN objEnfoqueOriginal = (clsEnfoqueMercadoVenta_CRN)(bsEnfoqueVenta.Current);
                clsEnfoqueDeMercado_CRN.CopiaDeEnfoque = objEnfoqueOriginal.Clone();
            }
        }

        private void btnPegar_Click(object sender, EventArgs e)
        {
            if (bsEnfoqueVenta.Current != null)
            {
                clsEnfoqueMercadoVenta_CRN objEnfoqueOriginal = (clsEnfoqueMercadoVenta_CRN)(bsEnfoqueVenta.Current);
                objEnfoqueOriginal.Pegar(clsEnfoqueDeMercado_CRN.CopiaDeEnfoque);
                this.bsEnfoqueVenta.ResetBindings(false);
            }
        }

        private void superficieTotalDeConstruccionTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            vs = new VSimbolo();
            e.Handled = vs.esDecimal(e.KeyChar, (superficieTotalDeConstruccionTextBox.Text).Trim());
        }

        private void valorHomologadoDeVentasAplicableTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            vs = new VSimbolo();
            e.Handled = vs.esDecimal(e.KeyChar, (valorHomologadoDeVentasAplicableTextBox1.Text).Trim());
        }

        private void valorHomologadoPorFactorATextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            vs = new VSimbolo();
            e.Handled = vs.esDecimal(e.KeyChar, (valorHomologadoPorFactorATextBox.Text).Trim());
        }

        private void valorDeVentasComparativoDeMercadoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            vs = new VSimbolo();
            e.Handled = vs.esDecimal(e.KeyChar, (valorDeVentasComparativoDeMercadoTextBox.Text).Trim());
        }

        private void gvDatosGenerales_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (this.gvDatosGenerales.CurrentCell.ColumnIndex != 1)
            {
                TextBox txt = (TextBox)e.Control;
                txt.KeyPress += validar_Keypress_gDatGen;
            }
        }
        private void validar_Keypress_gDatGen(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            int col = gvDatosGenerales.CurrentCell.ColumnIndex;
            if (col == 6 )
            {
                char caracter = e.KeyChar;
                // referencia a la celda   
                TextBox txt = (TextBox)sender;
                vs = new VSimbolo();
                e.Handled = vs.esDecimal(caracter, (txt.Text).Trim());
            }
        }
        private void gvDatosGenerales_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            string dato = e.FormattedValue.ToString();
            val = new Validadora();
            int gv = gvDatosGenerales.Columns[e.ColumnIndex].Index;
            if (gv == 6 )
            {
                if (!val.esReal(dato))
                {
                    e.Cancel = true;
                }
            }
        }

        private void gvPrecios_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox txt = (TextBox)e.Control;
            txt.KeyPress += validar_Keypress_gvPrecios; 
        }
        private void validar_Keypress_gvPrecios(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            int c = gvPrecios.CurrentCell.ColumnIndex;
            if (c == 1 || c == 2 || c == 3 || c == 4 || c == 5 || c == 6 || c == 7 || c == 8 || c == 9 || c == 10 || c == 11 || c == 12 || c == 13 || c == 14 || c == 15)
            {
                char caracter = e.KeyChar;
                // referencia a la celda   
                TextBox txt = (TextBox)sender;
                vs = new VSimbolo();
                e.Handled = vs.esDecimal(caracter, (txt.Text).Trim());
            }
        }

        private void gvPrecios_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            string dato = e.FormattedValue.ToString();
            val = new Validadora();
            int gv = gvPrecios.Columns[e.ColumnIndex].Index;
            if (gv == 1 || gv == 2 || gv == 3 || gv == 4 || gv == 5 || gv == 6 || gv == 7 || gv == 8 || gv == 9 || gv == 10 || gv == 11 || gv == 12 || gv == 13 || gv == 14 || gv == 15)
            {
                if (!val.esReal(dato))
                {
                    e.Cancel = true;
                }
            }
        }

    }
}
